package zjz.graduation.backend.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zjz.graduation.backend.dto.CaseMessage;
import zjz.graduation.backend.dto.GeneralMessage;
import zjz.graduation.backend.model.Cases;
import zjz.graduation.backend.model.Comments;
import zjz.graduation.backend.service.CasesService;
import zjz.graduation.backend.service.CommentsService;
import zjz.graduation.backend.service.UserService;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/case")
public class CasesController {
    @Autowired
    CasesService casesService;
    @Autowired
    UserService userService;
    @Autowired
    CommentsService commentsService;

    //等价@PostMapping("/login")
    @RequestMapping(method = RequestMethod.GET)
    public GeneralMessage getAllCases(){
        GeneralMessage message = new GeneralMessage();
        message.setMessage( casesService.getAllCases());
        return message;
    }

    @RequestMapping(method = RequestMethod.POST)
    public GeneralMessage createCase(@RequestBody Cases cases){
        GeneralMessage message = new GeneralMessage();
        message.setMessage( casesService.createCase(cases));
        return message;
    }

    //等价@PostMapping("/login")
    @RequestMapping(value = "/case",method = RequestMethod.GET)
    public CaseMessage getCase(@RequestParam Integer cid){
        CaseMessage message = new CaseMessage();
        Cases cases = casesService.getCase(cid);
        if(cases!=null){
            message.setMessage(cases);
            message.setAuthor(userService.getUserNameByUid(cases.getUid()));
            List<Comments> comments = commentsService.getCommentsByCid(cid);
            for(Comments comment: comments){
                comment.setAuthor(userService.getUserNameByUid(comment.getUid()));
            }
            message.setComments(comments);
        }else {
            message.setState(false);
            message.setCode(40000);
        }
        return message;
    }

    @RequestMapping(value = "/unreviewed",method = RequestMethod.GET)
    public GeneralMessage getUnreviewedCase(){
        GeneralMessage message = new GeneralMessage();
        message.setMessage( casesService.getUnreviewedCases());
        return message;
    }

    @RequestMapping(value = "/pass", method = RequestMethod.POST)
    public GeneralMessage passCases(@RequestBody Map map){
        GeneralMessage message = new GeneralMessage();
        casesService.updateStatus((Integer) map.get("cid"),1);
        return message;
    }

    @RequestMapping(value = "/unpass", method = RequestMethod.POST)
    public GeneralMessage unpassCases(@RequestBody Map map){
        GeneralMessage message = new GeneralMessage();
        casesService.updateStatus((Integer) map.get("cid"),2);
        return message;
    }

    @RequestMapping(value = "/insertComment", method = RequestMethod.POST)
    public GeneralMessage insertComment(@RequestBody Comments comments){
        GeneralMessage message = new GeneralMessage();
        commentsService.insertComment(comments);
        return message;
    }
}
